package org.jeecg.modules.demo.pcl.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.query.QueryRuleEnum;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.pcl.entity.MesAlmTpmcount;
import org.jeecg.modules.demo.pcl.mapper.MesAlmTpmcountMapper;
import org.jeecg.modules.demo.pcl.service.IMesAlmTpmcountService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: mes-tpm-评分均值
 * @Author: jeecg-boot
 * @Date:   2025-02-18
 * @Version: V1.0
 */
@Api(tags="mes-tpm-评分均值")
@RestController
@RequestMapping("/pcl/mesAlmTpmcount")
@Slf4j
public class MesAlmTpmcountController extends JeecgController<MesAlmTpmcount, IMesAlmTpmcountService> {
	@Autowired
	private IMesAlmTpmcountService mesAlmTpmcountService;
	 @Autowired
	 private MesAlmTpmcountMapper mesAlmTpmcountMapper;
	/**
	 * 分页列表查询
	 */
	//@AutoLog(value = "mes-tpm-评分均值-分页列表查询")
	@ApiOperation(value="mes-tpm-评分均值-分页列表查询", notes="mes-tpm-评分均值-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<MesAlmTpmcount>> queryPageList(
			@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
			@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
			@RequestParam(name="belongMonth", required=false) String belongMonth,
			HttpServletRequest req) throws ParseException {
		IPage<MesAlmTpmcount> pageList;
		if (belongMonth != null) {
			pageList = mesAlmTpmcountService.getCustomPageList(pageNo, pageSize, belongMonth);
		} else {
			pageList = mesAlmTpmcountService.getCustomPageList(pageNo, pageSize, "2025-02"); // 默认值
		}
		return Result.OK(pageList);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param mesAlmTpmcount
    */
    @RequiresPermissions("pcl:mes_alm_tpmcount:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, MesAlmTpmcount mesAlmTpmcount,
								  @RequestParam(name="belongMonth", required=false) String belongMonth) throws ParseException {
		//belongMonth参数为空时，默认为2025-02
		if (belongMonth == null) {
			//前端打印异常
			throw new ParseException("请输入所属年月！", 0);
		}
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
		Date date = null;
		date = dateFormat.parse(belongMonth);
		mesAlmTpmcount.setBelongMonth(date);

		// 自定义SQL查询逻辑
		List<MesAlmTpmcount> list = mesAlmTpmcountService.getCustomList(belongMonth);

		// 使用父类的exportXls方法导出Excel
//		return super.exportXls(request, list, MesAlmTpmcount.class, "mes-tpm-评分均值");

		// 若父类无对应方法，改用原生导出逻辑：
		 ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		 mv.addObject(NormalExcelConstants.DATA_LIST, list);
		 mv.addObject(NormalExcelConstants.CLASS, MesAlmTpmcount.class);
		 mv.addObject(NormalExcelConstants.PARAMS, new ExportParams(belongMonth+"-tpm-评分均值", null, "导出信息"));
		 return mv;
    }

}
